package com.koron.order.common.jurisdiction;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.business.workflow.bean.po.SysWorkflow;
import com.koron.common.core.business.workflow.service.SysWorkflowService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.common.bean.base.JurisdictionUserType;
import com.koron.order.common.bean.dto.OrderJurisdictionDto;
import com.koron.order.common.bean.po.OrderDispatchWorker;
import com.koron.order.common.bean.po.OrderReassignPo;
import com.koron.order.common.bean.po.OrderWorkDeptPo;
import com.koron.order.common.bean.query.OrderJurisdictionQuery;
import com.koron.order.common.bean.vo.OrderDispatchVo;
import com.koron.order.common.controller.OrderDispatchController;
import com.koron.order.common.controller.OrderReassignController;
import com.koron.order.concession.bean.dto.ConcessionStatus;
import com.koron.order.concession.bean.po.ConcessionPo;
import com.koron.order.concession.controller.ConcessionController;
import com.koron.order.defect.bean.po.DefectStatus;
import com.koron.order.defect.bean.query.DefectQuery;
import com.koron.order.defect.bean.vo.DefectVO;
import com.koron.order.defect.controller.DefectController;
import com.koron.order.defect.controller.DefectDispatchController;
import com.koron.order.defectDemotion.bean.po.DefectDemotionPo;
import com.koron.order.defectDemotion.bean.po.DefectDemotionStatus;
import com.koron.order.defectDemotion.controller.DefectDemotionController;
import com.koron.order.defectPending.bean.po.DefectPendingPo;
import com.koron.order.defectPending.bean.po.DefectPendingStatus;
import com.koron.order.defectPending.controller.DefectPendingController;
import com.koron.order.experiment.bean.po.ExperimentStatus;
import com.koron.order.experiment.bean.vo.ExperimentDetailVo;
import com.koron.order.experiment.controller.ExperimentController;
import com.koron.order.maintain.bean.entity.MaintainStatus;
import com.koron.order.maintain.bean.vo.MaintainDetailVo;
import com.koron.order.maintain.controller.MaintainController;
import com.koron.order.overhaul.bean.dto.OverhaulStatus;
import com.koron.order.overhaul.bean.vo.OrderDispatchTeamVO;
import com.koron.order.overhaul.bean.vo.OrderDispatchVO;
import com.koron.order.overhaul.bean.vo.OverhaulOrderDetailVO;
import com.koron.order.overhaul.controller.OverhaulOrderController;
import com.koron.order.question.bean.dto.QuestionStatus;
import com.koron.order.question.bean.po.QuestionPo;
import com.koron.order.question.controller.QuestionController;
import com.koron.order.repair.bean.entity.RepairStatus;
import com.koron.order.repair.bean.vo.RepairDetailVo;
import com.koron.order.repair.controller.RepairController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName OrderJurisdiction
 * @description 工单权限
 * @Author zhouj
 * @Date 2022/3/12 14:47
 */
@Slf4j
@RequestMapping("/order/")
@RestController
@Api(tags = "查询工单页面的权限")
public class OrderJurisdiction {

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    @Autowired
    DefectController defectController;//缺陷工单主表

    @Autowired
    MaintainController maintainController;//维护工单主表

    @Autowired
    OverhaulOrderController overhaulController;//大修工单主表

    @Autowired
    DefectDispatchController defectDispatchController;//缺陷派工

    @Autowired
    OrderDispatchController orderDispatchController;//工单派工信息

    @Autowired
    OrderReassignController orderReassignController;//工单转派信息

    @Autowired
    ExperimentController experimentController;//试验工单主表

    @Autowired
    RepairController repairController;//计划检修工单主表

    @Autowired
    ConcessionController concessionController;//让步接收工单主表

    @Autowired
    QuestionController questionController;//重大问题工单主表

    @Autowired
    DefectDemotionController defectDemotionController;//缺陷降级工单主表

    @Autowired
    DefectPendingController defectPendingController;//缺陷待处理工单主表

    @Autowired
    SysWorkflowService sysWorkflowService;

    //根据工单类型 工单id 工单状态 定位到具体页面的按钮 tips:对应的页面上的按钮权限
    @GetMapping("getOrderJurisdiction")
    @ApiOperation(value = "查询页面的按钮权限")
    public Response<Object> getOrderJurisdiction(OrderJurisdictionQuery orderJurisdictionQuery) {

        String orderId = orderJurisdictionQuery.getOrderId();
        String orderType = orderJurisdictionQuery.getOrderType();
        String orderStatus = orderJurisdictionQuery.getOrderStatus();

        Map<String, List<String>> pageButtonCodeMap = getPageButtonCodeMap(orderType, orderStatus);

        if (MapUtils.isEmpty(pageButtonCodeMap)) {
            return Response.ok("没有为该工单状态的页面配置权限");
        }
        return Response.ok(getJurisdiction(orderJurisdictionQuery, pageButtonCodeMap));
    }

    //获取各个工单权限清单
    public List<OrderJurisdictionDto> getJurisdiction(OrderJurisdictionQuery orderJurisdictionQuery, Map<String, List<String>> pageButtonCodeMap) {

        // 按钮列表为空 直接返回
        if (MapUtils.isEmpty(pageButtonCodeMap)) {
            return new ArrayList<>();
        }

        List<OrderJurisdictionDto> orderJurisdictionDtoList = new ArrayList<>();
        Map<String, List<String>> cond = new HashMap<>();

        switch (orderJurisdictionQuery.getOrderType()) {
            case "defect":
                cond = getDefect(orderJurisdictionQuery);
                break;
            case "maintain":
                cond = getMaintain(orderJurisdictionQuery);
                break;
            case "experiment":
                cond = getExperiment(orderJurisdictionQuery);
                break;
            case "overhaul":
                cond = getOverhaul(orderJurisdictionQuery);
                break;
            case "repair":
                cond = getRepair(orderJurisdictionQuery);
                break;
            case "concession":
                cond = getConcession(orderJurisdictionQuery);
                break;
            case "question":
                cond = getQuestion(orderJurisdictionQuery);
                break;
            case "defectDemotion":
                cond = getDefectDemotion(orderJurisdictionQuery);
                break;
            case "defectPending":
                cond = getDefectPending(orderJurisdictionQuery);
                break;
            default:
                return orderJurisdictionDtoList;
        }
        //按钮清单对应的操作人清单--依次判断当前用户是否拥有权限操作
        Iterator<Map.Entry<String, List<String>>> it = pageButtonCodeMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, List<String>> next = it.next();
            List<String> value = next.getValue();
            orderJurisdictionDtoList.add(new OrderJurisdictionDto(next.getKey(), hasJurisdiction(cond, value), getRule(value)));
        }
        return orderJurisdictionDtoList;
    }

    private Map<String, List<String>> getDefectPending(OrderJurisdictionQuery orderJurisdictionQuery) {
        // 1.1 构建返回对象
        Map<String, List<String>> cond = new HashMap<>();

        // 1.2 维护工单通过id查询维护工单主表信息
        Response<DefectPendingPo> response = defectPendingController.queryById(orderJurisdictionQuery.getOrderId());
        DefectPendingPo defectPendingPo = (DefectPendingPo) response.getData();

        // 1.3 根据工单id查询派工信息Map
        Map<String, List<String>> dispatchMap = getDispatchMap(orderJurisdictionQuery.getOrderId());

        /*----------------------基础数据已准备好,下面构建返回信息------------------------*/
        // 2.1 创建人所在单位
        cond.put("createOrg", new ArrayList<String>() {{
            add(defectPendingPo.getDeptId());
        }});
        // 2.3 工作负责人
        cond.put("deptWorkUser", dispatchMap.get("deptWorkUser"));
        // 2.4 工作班成员
        cond.put("worker", dispatchMap.get("worker"));
        // 2.5 工作跟踪负责人
        cond.put("leader", dispatchMap.get("leader"));
        // 2.6 工作跟踪负责人所在单位或者派工时的作业单位
        cond.put("leaderOrWorkerOrg", dispatchMap.get("leaderOrWorkerOrg"));

        return cond;
    }

    private Map<String, List<String>> getDefectDemotion(OrderJurisdictionQuery orderJurisdictionQuery) {
        // 1.1 构建返回对象
        Map<String, List<String>> cond = new HashMap<>();

        // 1.2 维护工单通过id查询维护工单主表信息
        Response<DefectDemotionPo> response = defectDemotionController.queryById(orderJurisdictionQuery.getOrderId());
        DefectDemotionPo defectDemotionPo = (DefectDemotionPo) response.getData();

        // 1.3 根据工单id查询派工信息Map
        Map<String, List<String>> dispatchMap = getDispatchMap(orderJurisdictionQuery.getOrderId());

        /*----------------------基础数据已准备好,下面构建返回信息------------------------*/
        // 2.1 创建人所在单位
        cond.put("createOrg", new ArrayList<String>() {{
            add(defectDemotionPo.getDeptId());
        }});
        // 2.3 工作负责人
        cond.put("deptWorkUser", dispatchMap.get("deptWorkUser"));
        // 2.4 工作班成员
        cond.put("worker", dispatchMap.get("worker"));
        // 2.5 工作跟踪负责人
        cond.put("leader", dispatchMap.get("leader"));
        // 2.6 工作跟踪负责人所在单位或者派工时的作业单位
        cond.put("leaderOrWorkerOrg", dispatchMap.get("leaderOrWorkerOrg"));

        return cond;
    }

    private Map<String, List<String>> getQuestion(OrderJurisdictionQuery orderJurisdictionQuery) {
        // 1.1 构建返回对象
        Map<String, List<String>> cond = new HashMap<>();

        // 1.2 维护工单通过id查询维护工单主表信息
        Response<QuestionPo> response = questionController.queryById(orderJurisdictionQuery.getOrderId());
        QuestionPo questionPo = response.getData();

        // 1.3 根据工单id查询派工信息Map
        Map<String, List<String>> dispatchMap = getDispatchMap(orderJurisdictionQuery.getOrderId());

        /*----------------------基础数据已准备好,下面构建返回信息------------------------*/
        // 2.1 创建人所在单位
        cond.put("createOrg", new ArrayList<String>() {{
            add(questionPo.getDeptId());
        }});
        // 2.2 设备管理单位
        cond.put("deptManageOrg", new ArrayList<String>() {{
            add(questionPo.getDeptManage());
        }});
        // 2.3 工作负责人
        cond.put("deptWorkUser", dispatchMap.get("deptWorkUser"));
        // 2.4 工作班成员
        cond.put("worker", dispatchMap.get("worker"));
        // 2.5 工作跟踪负责人
        cond.put("leader", dispatchMap.get("leader"));
        // 2.6 工作跟踪负责人所在单位或者派工时的作业单位
        cond.put("leaderOrWorkerOrg", dispatchMap.get("leaderOrWorkerOrg"));

        return cond;
    }

    private Map<String, List<String>> getConcession(OrderJurisdictionQuery orderJurisdictionQuery) {

        // 1.1 构建返回对象
        Map<String, List<String>> cond = new HashMap<>();

        // 1.2 维护工单通过id查询维护工单主表信息
        Response<ConcessionPo> response = concessionController.queryById(orderJurisdictionQuery.getOrderId());
        ConcessionPo concessionPo = response.getData();

        // 1.3 根据工单id查询派工信息Map
        Map<String, List<String>> dispatchMap = getDispatchMap(orderJurisdictionQuery.getOrderId());

        /*----------------------基础数据已准备好,下面构建返回信息------------------------*/
        // 2.1 创建人所在单位
        cond.put("createOrg", new ArrayList<String>() {{
            add(concessionPo.getDeptId());
        }});
        // 2.2 设备管理单位
        cond.put("deptManageOrg", new ArrayList<String>() {{
            add(concessionPo.getDeptManage());
        }});
        // 2.3 工作负责人
        cond.put("deptWorkUser", dispatchMap.get("deptWorkUser"));
        // 2.4 工作班成员
        cond.put("worker", dispatchMap.get("worker"));
        // 2.5 工作跟踪负责人
        cond.put("leader", dispatchMap.get("leader"));
        // 2.6 工作跟踪负责人所在单位或者派工时的作业单位
        cond.put("leaderOrWorkerOrg", dispatchMap.get("leaderOrWorkerOrg"));

        return cond;
    }

    private Map<String, List<String>> getRepair(OrderJurisdictionQuery orderJurisdictionQuery) {

        // 1.1 构建返回对象
        Map<String, List<String>> cond = new HashMap<>();

        // 1.2 维护工单通过id查询维护工单主表信息
        Response<RepairDetailVo> response = repairController.queryById(orderJurisdictionQuery.getOrderId());
        RepairDetailVo repairDetailVo = response.getData();

        // 1.3 根据工单id查询派工信息Map
        Map<String, List<String>> dispatchMap = getDispatchMap(orderJurisdictionQuery.getOrderId());

        // 1.4 根据工单id获取流程执行信息
        List<String> workOrg = new ArrayList<>();
        List<SysWorkflow> sysWorkflows = sysWorkflowService.queryByDataId(repairDetailVo.getId());
        String currNodeUserId = null;
        // 流程审批最后一个节点审批人
        if (CollectionUtil.isNotEmpty(sysWorkflows)){
            SysWorkflow sysWorkflow = sysWorkflows.get(0);
            currNodeUserId = sysWorkflow.getCurNodeUserId();
        }


        // 1.5 查询最新一条转派信息
        Response<OrderReassignPo> orderReassignPoResponse = orderReassignController.queryLatestByOrderId(orderJurisdictionQuery.getOrderId());
        OrderReassignPo orderReassignPo = orderReassignPoResponse.getData();
        String reassignWorker = null;

        //派工人员
        String[] dispathWorker = new String[]{};
        //有转派过,则取转派的人--否则取流程最后一个审批节点的人
        //有转派过,则取转派的人--否则取流程最后一个审批节点的人
        if (!Objects.isNull(orderReassignPo)) {
            //指定的转派负责人
            reassignWorker = orderReassignPo.getWorker();
        } else {
            reassignWorker = currNodeUserId;
        }
        //当前节点处理人
        if (StringUtils.isNotEmpty(reassignWorker)) {
            dispathWorker = reassignWorker.split(",");
        }

        /*----------------------基础数据已准备好,下面构建返回信息------------------------*/
        // 2.1 创建人所在单位
        cond.put("createOrg", new ArrayList<String>() {{
            add(repairDetailVo.getDeptId());
        }});
        // 2.2 设备管理单位
        cond.put("deptManageOrg", new ArrayList<String>() {{
            add(repairDetailVo.getDeptManage());
        }});
        // 2.3 工作负责人
        cond.put("deptWorkUser", dispatchMap.get("deptWorkUser"));
        // 2.4 工作班成员
        cond.put("worker", dispatchMap.get("worker"));
        // 2.5 工作跟踪负责人
        cond.put("leader", dispatchMap.get("leader"));
        // 2.6 工作跟踪负责人所在单位或者派工时的作业单位
        cond.put("leaderOrWorkerOrg", dispatchMap.get("leaderOrWorkerOrg"));
        // 2.7 工作流审核人员
        cond.put("workFlowAudit", CollectionUtil.newArrayList(dispathWorker));

        return cond;
    }

    private String getRule(List<String> value) {
        StringBuilder sb = new StringBuilder();
        sb.append("只有以下人员有权限:");
        for (String userType : value) {
            sb.append(JurisdictionUserType.getDescByCode(userType));
            sb.append(",");
        }
        return sb.substring(0, sb.length() - 1);
    }

    /*
     * 功能描述 工单类型+工单状态--->确定页面的按钮清单
     * @author zhouj
     * @date 2022/6/6 15:26
     * @param orderType orderStatus
     * @return
     */
    public Map<String, List<String>> getPageButtonCodeMap(String orderType, String orderStatus) {
        switch (orderType) {
            case "defect":
                return DefectStatus.getPageButtonCode(orderStatus);
            case "maintain":
                return MaintainStatus.getPageButtonCode(orderStatus);
            case "experiment":
                return ExperimentStatus.getPageButtonCode(orderStatus);
            case "overhaul":
                return OverhaulStatus.getPageButtonCode(orderStatus);
            case "repair":
                return RepairStatus.getPageButtonCode(orderStatus);
            case "concession":
                return ConcessionStatus.getPageButtonCode(orderStatus);
            case "question":
                return QuestionStatus.getPageButtonCode(orderStatus);
            case "defectDemotion":
                return DefectDemotionStatus.getPageButtonCode(orderStatus);
            case "defectPending":
                return DefectPendingStatus.getPageButtonCode(orderStatus);
            default:
                return new HashMap<>();
        }
    }

    /*
     * 功能描述 缺陷工单的处理人
     * @author zhouj
     * @date 2022/4/19 10:43
     * @param   通过工单的id查询业务数据
     * @return 每类处理人类型--具体的人或者组织
     */
    public Map<String, List<String>> getDefect(OrderJurisdictionQuery orderJurisdictionQuery) {

        Map<String, List<String>> cond = new HashMap<>();

        //查询基础信息
        DefectQuery query = new DefectQuery();
        query.setId(orderJurisdictionQuery.getOrderId());
        Response<PageInfo<DefectVO>> pageInfoResponse = defectController.queryDefectVoList(query, new PageQuery());
        PageInfo<DefectVO> data = pageInfoResponse.getData();
        List<DefectVO> list = data.getList();
        DefectVO defectVO = list.get(0);

        //查询最新一条转派信息
        Response<OrderReassignPo> orderReassignPoResponse = orderReassignController.queryLatestByOrderId(orderJurisdictionQuery.getOrderId());
        OrderReassignPo orderReassignPo = orderReassignPoResponse.getData();
        String reassignWorker = null;

        //当前节点处理人
        String currNodeUser = defectVO.getCurrNodeUserId();
        //派工人员
        String[] dispathWorker = new String[]{};
        //有转派过,则取转派的人--否则取流程最后一个审批节点的人
        if (!Objects.isNull(orderReassignPo)) {
            //指定的转派负责人
            reassignWorker = orderReassignPo.getWorker();
        } else {
            reassignWorker = currNodeUser;
        }
        //当前节点处理人
        if (StringUtils.isNotEmpty(reassignWorker)) {
            dispathWorker = reassignWorker.split(",");
        }
        //调用派工接口  查询派工信息
        Map<String, List<String>> dispatchMap = getDispatchMap(orderJurisdictionQuery.getOrderId());

        // 2.1 创建人所在单位
        cond.put("createOrg", new ArrayList<String>() {{
            add(defectVO.getDeptId());
        }});
        // 2.2 设备管理部门
        cond.put("deptManageOrg", new ArrayList<String>() {{
            add(defectVO.getDeptManage());
        }});
        // 2.3 工作流审核人员
        cond.put("workFlowAudit", Arrays.asList(dispathWorker));
        // 2.4 工作负责人
        cond.put("deptWorkUser", dispatchMap.get("deptWorkUser"));
        // 2.5 工作班成员
        cond.put("worker", dispatchMap.get("worker"));
        // 2.6 工作跟踪负责人
        cond.put("leader", dispatchMap.get("leader"));
        // 2.7 工作跟踪负责人所在单位或者派工时的作业单位
        cond.put("leaderOrWorkerOrg", dispatchMap.get("leaderOrWorkerOrg"));
        return cond;

    }

    /*
     * 功能描述 正常维护工单的处理人
     * @author zhouj
     * @date 2022/4/19 10:43
     * @param   通过工单的id查询业务数据
     * @return 每类处理人类型--具体的人或者组织
     */
    public Map<String, List<String>> getMaintain(OrderJurisdictionQuery orderJurisdictionQuery) {

        // 1.1 构建返回对象
        Map<String, List<String>> cond = new HashMap<>();

        // 1.2 维护工单通过id查询维护工单主表信息
        Response<MaintainDetailVo> response = maintainController.queryById(orderJurisdictionQuery.getOrderId());
        MaintainDetailVo maintainDetailVo = response.getData();

        // 1.3 根据工单id查询派工信息Map
        Map<String, List<String>> dispatchMap = getDispatchMap(orderJurisdictionQuery.getOrderId());


        // 1.4 查询最新一条转派信息
        Response<OrderReassignPo> orderReassignPoResponse = orderReassignController.queryLatestByOrderId(orderJurisdictionQuery.getOrderId());
        OrderReassignPo orderReassignPo = orderReassignPoResponse.getData();

        // 取出工作流审批信息
        SysWorkflow workflow = new SysWorkflow();
        workflow.setDataId(orderJurisdictionQuery.getOrderId());
        List<SysWorkflow> sysWorkflows = sysWorkflowService.queryAll(workflow);

        String reassignWorker = null;

        //派工人员
        String[] dispathWorker = new String[]{};
        //有转派过,则取转派的人--否则取流程最后一个审批节点的人
        if (!Objects.isNull(orderReassignPo)) {
            //指定的转派负责人
            reassignWorker = orderReassignPo.getWorker();
            if (StringUtils.isNotEmpty(reassignWorker)) {
                dispathWorker = reassignWorker.split(",");
            }
        }else if(CollectionUtil.isNotEmpty(sysWorkflows)){
        //如果没有转派过，且流程已完成，设置为最后一个节点的审批人
            if(sysWorkflows.get(0).getIsFinished() == 1){
                dispathWorker = sysWorkflows.get(0).getCurNodeUserId().split(",");
            }
        }

        /*----------------------基础数据已准备好,下面构建返回信息------------------------*/
        // 2.1 创建人所在单位
        cond.put("createOrg", new ArrayList<String>() {{
            add(maintainDetailVo.getDeptId());
        }});
        // 2.2 设备管理单位
        cond.put("deptManageOrg", new ArrayList<String>() {{
            add(maintainDetailVo.getDeptManage());
        }});
        // 2.3 工作负责人
        cond.put("deptWorkUser", dispatchMap.get("deptWorkUser"));
        // 2.4 工作班成员
        cond.put("worker", dispatchMap.get("worker"));
        // 2.5 工作跟踪负责人
        cond.put("leader", dispatchMap.get("leader"));
        // 2.6 工作跟踪负责人所在单位或者派工时的作业单位
        cond.put("leaderOrWorkerOrg", dispatchMap.get("leaderOrWorkerOrg"));
        // 2.7 工作班组组织
        cond.put("workOrg", dispatchMap.get("workOrg"));
        // 2.8 工单转派人员
        cond.put("workFlowAudit", CollectionUtil.newArrayList(dispathWorker));

        return cond;

    }

    /*
     * 功能描述 大修工单的处理人
     * @author zhouj
     * @date 2022/4/19 10:43
     * @param   通过工单的id查询业务数据
     * @return 每类处理人类型--具体的人或者组织
     */
    public Map<String, List<String>> getOverhaul(OrderJurisdictionQuery orderJurisdictionQuery) {

        // 构建返回对象
        Map<String, List<String>> cond = new HashMap<>();

        // 获取大修工单基础信息
        Response<OverhaulOrderDetailVO> response = overhaulController.queryById(orderJurisdictionQuery.getOrderId());
        OverhaulOrderDetailVO data = response.getData();

        //工单派工信息
        OrderDispatchVO orderDispatchVo = data.getDispatch();

        //工作跟踪负责人
        String leader = "";
        //工作负责人
        String deptWorkUser = "";
        //跟踪负责人所在单位用户或作业单位用户
        List<String> leaderOrWorkerOrg = new ArrayList<>();
        List<OrderDispatchWorker> orderDispatchWorker = new ArrayList<>();


        List<String> workOrg = new ArrayList<>();
        List<SysWorkflow> sysWorkflows = sysWorkflowService.queryByDataId(data.getId());
        String currNodeUserId = null;
        // 流程审批最后一个节点审批人
        if (CollectionUtil.isNotEmpty(sysWorkflows)){
            SysWorkflow sysWorkflow = sysWorkflows.get(0);
            currNodeUserId = sysWorkflow.getCurNodeUserId();
        }

        //查询最新一条转派信息
        Response<OrderReassignPo> orderReassignPoResponse = orderReassignController.queryLatestByOrderId(orderJurisdictionQuery.getOrderId());
        OrderReassignPo orderReassignPo = orderReassignPoResponse.getData();
        String reassignWorker = null;

        String[] dispathWorker = new String[]{};
        //有转派过,则取转派的人--否则取流程最后一个审批节点的人
        if (!Objects.isNull(orderReassignPo)) {
            //指定的转派负责人
            reassignWorker = orderReassignPo.getWorker();
        } else {
            reassignWorker = currNodeUserId;
        }
        //当前节点处理人
        if (StringUtils.isNotEmpty(reassignWorker)) {
            dispathWorker = reassignWorker.split(",");
        }

        // 工作班组单位
        if (!Objects.isNull(orderDispatchVo)) {
            Integer isOut = orderDispatchVo.getIsOut();
            if (isOut != null && isOut == 0) {
                //内单位 工作负责人
                deptWorkUser = orderDispatchVo.getLeader();
                //是否外单位？是：跟踪责任人所在单位用户，否：作业单位用户。
                //工作班组列表
                List<OrderDispatchTeamVO> teams = orderDispatchVo.getTeams();
                if (CollectionUtils.isNotEmpty(teams)) {
                    for (OrderDispatchTeamVO team : teams) {
                        //班组里的作业单位列表
                        List<OrderWorkDeptPo> orderWorkDeptPoList = team.getWorkerDept();
                        if (CollectionUtils.isNotEmpty(orderWorkDeptPoList)) {
                            orderWorkDeptPoList.forEach(p -> {
                                leaderOrWorkerOrg.add(p.getWorkDept());
                            });
                        }
                    }
                }
            } else {
                //外单位 工作负责人
                deptWorkUser = orderDispatchVo.getOutLeader();
                leader = orderDispatchVo.getLeader();
                leaderOrWorkerOrg.add(orderDispatchVo.getDeptWork());
            }
            //工作班成员
            List<OrderDispatchTeamVO> teams = orderDispatchVo.getTeams();
            if (CollectionUtils.isNotEmpty(teams)) {
                for (OrderDispatchTeamVO team : teams) {
                    //班组里的作业单位列表
                    List<OrderDispatchWorker> workers = team.getWorkers();
                    if (CollectionUtils.isNotEmpty(workers)) {
                        orderDispatchWorker.addAll(workers);
                    }
                }
            }
            // 工作班组单位

            if (CollectionUtils.isNotEmpty(teams)) {
                for (OrderDispatchTeamVO team : teams) {
                    //班组里的作业单位列表
                    List<OrderWorkDeptPo> orderWorkDeptPoList = team.getWorkerDept();
                    if (CollectionUtils.isNotEmpty(orderWorkDeptPoList)) {
                        orderWorkDeptPoList.forEach(p -> {
                            workOrg.add(p.getWorkDept());
                        });
                    }
                }
            }
        }

        List<String> workerList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(orderDispatchWorker)) {
            workerList = orderDispatchWorker.stream().map(OrderDispatchWorker::getWorker).collect(Collectors.toList());
        }


        /*----------------------基础数据已准备好,下面构建返回信息------------------------*/
        // 2.1 创建人所在单位
        cond.put("createOrg", new ArrayList<String>() {{
            add(data.getDeptId());
        }});
        // 2.2 设备管理单位
        cond.put("deptManageOrg", new ArrayList<String>() {{
            add(data.getDeptManage());
        }});
        String finalDeptWorkUser = deptWorkUser;
        // 2.3 工作负责人
        cond.put("deptWorkUser", new ArrayList<String>() {{
            add(finalDeptWorkUser);
        }});
        // 2.4 工作班成员
        cond.put("worker", workerList);
        String finalLeader = leader;
        // 2.5 工作跟踪负责人
        cond.put("leader", new ArrayList<String>() {{
            add(finalLeader);
        }});
        // 2.6 工作跟踪负责人所在单位或者派工时的作业单位
        cond.put("leaderOrWorkerOrg", leaderOrWorkerOrg);

        // 2.7 工作班组单位
        cond.put("workOrg",workOrg);

        // 2.8 工作流审核人员
        cond.put("workFlowAudit", CollectionUtil.newArrayList(dispathWorker));

        return cond;

    }

    /*
     * 功能描述 试验工单的处理人
     * @author zhouj
     * @date 2022/4/19 10:43
     * @param   通过工单的id查询业务数据
     * @return 每类处理人类型--具体的人或者组织
     */
    public Map<String, List<String>> getExperiment(OrderJurisdictionQuery orderJurisdictionQuery) {

        // 1.1 构建返回对象
        Map<String, List<String>> cond = new HashMap<>();
        // 1.2 获取试验工单基础信息
        Response<ExperimentDetailVo> response = experimentController.queryById(orderJurisdictionQuery.getOrderId());
        ExperimentDetailVo experimentDetailVo = response.getData();

        // 1.3 获取派工信息(公共)
        Map<String, List<String>> dispatchMap = getDispatchMap(orderJurisdictionQuery.getOrderId());

        /*----------------------基础数据已准备好,下面构建返回信息------------------------*/
        // 2.1 创建人所在单位
        cond.put("createOrg", new ArrayList<String>() {{
            add(experimentDetailVo.getDeptId());
        }});
        // 2.2 设备管理单位
        cond.put("deptManageOrg", new ArrayList<String>() {{
            add(experimentDetailVo.getDeptManage());
        }});
        // 2.3 工作负责人
        cond.put("deptWorkUser", dispatchMap.get("deptWorkUser"));
        // 2.4 工作班成员
        cond.put("worker", dispatchMap.get("worker"));
        // 2.5 工作跟踪负责人
        cond.put("leader", dispatchMap.get("leader"));
        // 2.6 工作跟踪负责人所在单位或者派工时的作业单位
        cond.put("leaderOrWorkerOrg", dispatchMap.get("leaderOrWorkerOrg"));

        return cond;
    }

    /*
     * 功能描述 根据工单id查询派工信息
     * @author zhouj
     * @date 2022/6/6 15:03
     * @param orderId
     * @return Map<String, List<String>> 派工信息map
     */
    public Map<String, List<String>> getDispatchMap(String orderId) {

        // 构建返回对象
        Map<String, List<String>> map = new HashMap<>();

        //1.工单派工信息(通用)
        Response<OrderDispatchVo> orderDispatchVoResponse = orderDispatchController.queryDispatchInfoByOrderId(orderId);
        OrderDispatchVo orderDispatchVo = orderDispatchVoResponse.getData();
        //1.1 工作跟踪负责人
        List<String> leaderList = new ArrayList<>();
        //1.2 工作负责人
        List<String> deptWorkUserList = new ArrayList<>();
        //1.3 跟踪负责人所在单位用户或作业单位用户
        List<String> leaderOrWorkerOrg = new ArrayList<>();
        List<OrderDispatchWorker> orderDispatchWorker = new ArrayList<>();

        if (!Objects.isNull(orderDispatchVo)) {
            Integer isOut = orderDispatchVo.getIsOut();
            if (isOut != null && isOut == 0) {
                //内单位 工作负责人
                deptWorkUserList.add(orderDispatchVo.getLeader());
                //是否外单位？是：跟踪责任人所在单位用户，否：作业单位用户。
                List<OrderWorkDeptPo> orderWorkDeptPoList = orderDispatchVo.getOrderWorkDeptPoList();
                if (CollectionUtils.isNotEmpty(orderWorkDeptPoList)) {
                    orderWorkDeptPoList.forEach(p -> {
                        leaderOrWorkerOrg.add(p.getWorkDept());
                    });
                }
            } else {
                //外单位 工作负责人
                deptWorkUserList.add(orderDispatchVo.getOutLeader());
                leaderList.add(orderDispatchVo.getLeader());
                leaderOrWorkerOrg.add(orderDispatchVo.getDeptWork());
            }
            //工作班成员
            orderDispatchWorker = orderDispatchVo.getOrderDispatchWorker();
        }

        //1.4 工作班成员列表
        List<String> workerList = new ArrayList<>();
        List<String> workerOrgList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(orderDispatchWorker)) {
            workerList = orderDispatchWorker.stream().map(OrderDispatchWorker::getWorker).collect(Collectors.toList());
            workerOrgList = orderDispatchWorker.stream().map(OrderDispatchWorker::getJobDept).collect(Collectors.toList());
        }

        /*----------------------基础数据已准备好,下面构建返回信息------------------------*/
        map.put("leader", leaderList);
        map.put("deptWorkUser", deptWorkUserList);
        map.put("leaderOrWorkerOrg", leaderOrWorkerOrg);
        map.put("worker", workerList);
        map.put("workOrg", workerOrgList);

        return map;
    }

    /*
     * 功能描述 公共方法 判断是否有权限
     * @author zhouj
     * @date 2022/6/6 15:36
     * @param [cond 按钮权限人员清单, userTypeList 权限人员类型]
     * @return java.lang.Boolean
     */
    public Boolean hasJurisdiction(Map<String, List<String>> cond, List<String> userTypeList) {

        Boolean hasJurisdiction = false;

        String currOrg = getCurrUser().getCurrOrg();
        String account = getCurrUser().getAccount();

        for (String type : userTypeList) {
            //类型为创建单位,设备管理单位,作业单位或跟踪负责人单位 时,比较当前登录人的组织
            if (StringUtils.equalsAnyIgnoreCase(type, "createOrg", "deptManageOrg", "leaderOrWorkerOrg","workOrg")) {
                if (cond.get(type).contains(currOrg)) {
                    return true;
                }
            } else if (StringUtils.equalsAnyIgnoreCase(type, "workFlowAudit", "deptWorkUser", "worker", "leader")) {
                //类型为工作流最后一个节点审核人,工作负责人,工作班成员,工作跟踪负责人 时,比较当前登录人的账号
                if (cond.get(type).contains(account)) {
                    return true;
                }
            }
        }
        return hasJurisdiction;
    }

}
